
import { notification, Icon } from 'ant-design-vue'
import getDataForDB from '@/api/Notification'
/**
 * 消息类型
 */
const notificationType = {
  // 101: '客户入驻',
  // 201: '用工场景',
  // 301: '合同签署',
  // 401: '任务广场',
  // 501: '发票申请',
  // 601: '业务结算',
  // 701: '黑名单',
  // 801: '白名单',
  101: 101,
  201: 201,
  301: 301,
  401: 401,
  501: 501,
  601: 601,
  701: 701,
  801: 801,
}

/**
 * 消息类型图标
 */
const notificationIcon = {
  101: (h) => { return <a-icon type="bell" style="color: #108ee9" /> },
  201: (h) => { return <a-icon type="bell" style="color: #108ee9" /> },
  301: (h) => { return <a-icon type="bell" style="color: #108ee9" /> },
  401: (h) => { return <Icon type="sound" style="color: #108ee9"></Icon> },
  501: (h) => { return <Icon type="sound" style="color: #108ee9"></Icon> },
  601: (h) => { return <Icon type="sound" style="color: #108ee9"></Icon> },
  701: (h) => { return <Icon type="sound" style="color: #108ee9"></Icon> },
  801: (h) => { return <Icon type="sound" style="color: #108ee9"></Icon> },
  "默认": (h) => { return <Icon type="sound" style="color: #108ee9"></Icon> },
}

/**
 * 消息内容模板
 */
const notificationMesg = {
  101: (h, notificationItem) => { return notificationItem.msgContent },
  201: (h, notificationItem) => { return notificationItem.msgContent },
  301: (h, notificationItem) => { return notificationItem.msgContent },
  401: (h, notificationItem) => { return notificationItem.msgContent },
  501: (h, notificationItem) => { return notificationItem.msgContent },
  601: (h, notificationItem) => { return notificationItem.msgContent },
  701: (h, notificationItem) => { return notificationItem.msgContent },
  801: (h, notificationItem) => { return notificationItem.msgContent },
  "默认": (h, notificationItem) => { return notificationItem.msgContent },
}
const notificationTitle = {

}


/**
 * 获取消息标题
 * @param {*} typeCode 消息类型编码
 */
const getNotificationTitle = (typeCode, notificationItem) => {
  const tempTitle = notificationItem.bizTypeName
  if (tempTitle) {
    return `新消息--${tempTitle}`
  } else {
    return `新消息`
  }
}

/**
 * 获取消息图标
 * @param {*} typeCode 消息类型编码
 */
const getNotificationIcon = (h, typeCode, notificationItem) => {
  let code = typeCode
  const { bizType } = notificationItem
  code = bizType
  return notificationIcon[code] && notificationIcon[code](h, notificationItem) || notificationIcon['默认'](h, notificationItem)
}

/**
 * 获取消息内容
 * @param {*} h
 * @param {*} typeCode 消息类型编码
 * @returns
 */
const getNotificationMesg = (h, typeCode, notificationItem) => {
  // /taskManagement/EmploymentScenario
  return <div>
    {notificationMesg[typeCode](h, notificationItem)}
  </div>
}


window['NotificationCount'] = 0

class Notification {
  duration_ = 3
  typeCode_ = Object.keys(notificationType)
  suspendTime_ = 0
  pageSelf_ = null
  Notification(duration) {
    // this.duration_ = duration || 3
  }
  init(pageSelf) {
    this.pageSelf_ = pageSelf

    // 暂时关闭消息轮训
    this.createTimer()
  }

  createTimer() {
    setInterval(() => {
      // this.checkSuspendTime()
      // this.suspendTime_++
      this.getMesgDataList(3, 1)
    }, 10000);
  }

  async getMesgDataList(pageSize = 0, type = 0) {
    // "bizType": 0,
    const param = {
      "data": {
        "disType": 1,
        "isDis": 0,
        "isRead": 0
      },
      "pageNo": 1,
      "pageSize": pageSize
    }
    const res = await getDataForDB('消息列表查询接口', param)
    // console.log('消息列表查询接口====>', res);
    if (res.success) {
      const { result: { records, total } } = res
      const mesgCount = total || 0
      mesgCount > 0 && getDataForDB('弹出消息通知')
      // if (type === 0) {
      if (mesgCount > 3) {
        const notificationItem = {
          "id": 0, "msgSys": 2, "msgRange": 3, "msgRole": null,
          "msgContent": `您有${total >= 99 ? '99+' : total}条未读消息`, "bizType": '默认', "bizTypeName": "", "bizUrlFlag": 1,
          "bizUrl": "/settings/notification", "isRead": 0, "createBy": "",
          "createById": -1, "createTime": "", "updateBy": null, "updateTime": null,
          "updateById": null, "delFlag": 0
        }
        if (total > 0) {
          this.createNotification(notificationItem)
        }
      }

      // if (type === 1) {
      if (mesgCount<=3 && mesgCount > 0) {
        records.forEach(item => {
          this.createNotification(item)
        })

      }
    }
  }




  // checkSuspendTime() {
  //   const tempLenth = this.typeCode_.length
  //   if (this.suspendTime_ >= tempLenth) {
  //     this.suspendTime_ = 0
  //   }
  // }

  goto(notificationItem) {
    // /taskManagement/EmploymentScenario
    this.pageSelf_.$router.push({
      path: notificationItem.bizUrl
    })
  }

  createNotification(notificationItem) {
    const { bizType } = notificationItem
    const typeCode = notificationType[bizType] || '默认'//this.typeCode_[suspendTime_]
    notification.open({
      duration: this.duration_,
      message: (h) => {
        return getNotificationTitle(typeCode, notificationItem)
      },

      description: (h) => {
        return <div style={'cursor:pointer;'} onClick={() => { this.goto(notificationItem) }}>
          {getNotificationMesg(h, typeCode, notificationItem)}
        </div>

      },
      icon: (h) => {
        return getNotificationIcon(h, typeCode, notificationItem)
      }
    });
  }

}






export default new Notification()
